﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using CoreProject.IRepository;
using CoreProject.Model;
using Microsoft.EntityFrameworkCore;

namespace CoreProject.Repository
{
    public class GoodsRepository : BaseRepository<Good>, IGoodsRepository
    {
        private readonly EFDbContext _context;

        public GoodsRepository(EFDbContext context) : base(context)
        {
            this._context = context;
        }

        public async Task<bool> DeleteGood(int id)
        {
            var data =_context.Goods.Where(n => n.GId == id).FirstOrDefault();
            var keyDal = _context.GoodsPropertyKeys;
            var valueDal =_context.GoodsPropertyValues;
            foreach (var key in data.GoodsPropertyKeys)
            {
                valueDal.RemoveRange(key.GoodsPropertyValues.ToList());
            }
            keyDal.RemoveRange(data.GoodsPropertyKeys.ToList());
            _context.Goods.Remove(data);
            return await _context.SaveChangesAsync()>0;

        }

        public async Task<bool> DeleteGoods(int[] ids)
        {
            var data = _context.Goods.Include(n => n.GoodsPropertyKeys).ThenInclude(n => n.GoodsPropertyValues).Where(n => ids.Contains(n.GId)).ToList();
            var keyDal = _context.GoodsPropertyKeys;
            var valueDal =_context.GoodsPropertyValues;
            foreach (var good in data)
            {
                foreach (var key in good.GoodsPropertyKeys)
                {
                    valueDal.RemoveRange(key.GoodsPropertyValues.ToList());
                }

            }
            foreach (var good in data)
            {
                keyDal.RemoveRange(good.GoodsPropertyKeys.ToList());
            }
            _context.Goods.RemoveRange(data.ToList());
           
            return await _context.SaveChangesAsync()>0;


        }

        public async Task<(List<Good> list, int count)> GetGoodsBypage(string name, int page, int size)
        {
            var data = await _context.Goods.Include(g =>g.Gt).ThenInclude(g=>g.Goods).ThenInclude(g=>g.GoodsPropertyKeys).ThenInclude(g => g.Gsk).ThenInclude(g => g.GoodsPropertyKeys).ThenInclude(c => c.GoodsPropertyValues).ThenInclude(c => c.Gsv).ToListAsync();
            if (!string.IsNullOrWhiteSpace(name))
            {
                data = data.Where(n => n.Name.Contains(name)).ToList();
            }
            int count = data.Count();
            var pagelist = data.OrderBy(n => n.GId).Skip((page - 1) * size).Take(size);
            return (pagelist.ToList(), count);
        }

        public async Task<bool> ShangJia(int gid)
        {
            var zhi =await _context.Goods.Where(n => n.GId == gid).FirstOrDefaultAsync();
            zhi.OnSale = true;
            _context.Goods.Update(zhi);
         
            return await _context.SaveChangesAsync()>0;

        }

        public async Task<bool> UpdateGood(Good good)
        {
            
            var data =await _context.Goods.Include(n => n.GoodsPropertyKeys).ThenInclude(n => n.GoodsPropertyValues). Where(n => n.GId == good.GId).FirstOrDefaultAsync();
            data.Name = good.Name;
            data.Price = good.Price;
            data.Unit = good.Unit;
            data.GtId = good.GtId;
            data.UseProperty = good.UseProperty;
            data.UseSpec = false;
            data.Content = good.Content;
            data.Images = good.Images;
            data.Sort = good.Sort;
            var keyDal = _context.GoodsPropertyKeys;
            var valueDal = _context.GoodsPropertyValues;
            foreach (var key in data.GoodsPropertyKeys)
            {
                _context.GoodsPropertyValues.RemoveRange(key.GoodsPropertyValues.ToList());
            }
            keyDal.RemoveRange(data.GoodsPropertyKeys.ToList());
            keyDal.AddRange(good.GoodsPropertyKeys.ToList());
            _context.Goods.Update(data);
            return await _context.SaveChangesAsync()>0;

        }

        public async Task<bool> XiaJia(int gid)
        {
            var zhi = await _context.Goods.Where(n => n.GId == gid).FirstOrDefaultAsync();
            zhi.OnSale = false;
            _context.Goods.Update(zhi);
            return await _context.SaveChangesAsync() > 0;
        }
    }
}
